import java.util.ArrayList;
import java.util.List;

/**
 * @author: 最帅的的乐乐
 * @date: 2024-05-08 11:03 星期三
 * @motto the harder, the luckier。
 *
 *
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 *
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 *
 *
 *
 *
 *
 *
 *
 */

public class _17_LetterCombinations {
    private String letterMap[] = {
            " ",    //0
            "",     //1
            "abc",  //2
            "def",  //3
            "ghi",  //4
            "jkl",  //5
            "mno",  //6
            "pqrs", //7
            "tuv",  //8
            "wxyz"  //9
    };

    private ArrayList<String> res;


    public List<String> letterCombinations(String digits) {

        res = new ArrayList<String>();
        if(digits.equals(""))
            return res;

        findCombination(digits, 0, "");
        return res;
    }

    private void findCombination(String digits, int index, String s){

        if(index == digits.length()){
            res.add(s);
            return;
        }

        Character c = digits.charAt(index);
        String letters = letterMap[c - '0'];
        for(int i = 0 ; i < letters.length() ; i ++){
            findCombination(digits, index+1, s + letters.charAt(i));
        }

        return;
    }

    public static void main(String[] args) {
        _17_LetterCombinations solution = new _17_LetterCombinations();

        String digits1 = "0";
        System.out.println("Input: " + digits1);
        System.out.println("Output: " + solution.letterCombinations(digits1));

        String digits2 = "";
        System.out.println("\nInput: " + digits2);
        System.out.println("Output: " + solution.letterCombinations(digits2));

        String digits3 = "7";
        System.out.println("\nInput: " + digits3);
        System.out.println("Output: " + solution.letterCombinations(digits3));
    }

}
